package com.besttone.search.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;

import com.besttone.search.Client;
import com.besttone.search.model.City;
import com.besttone.search.model.PhoneInfo;
import com.besttone.search.sql.NativeDBHelper;

public class SharedUtils {
	public static final String Custom_Channel = "custom_channel";
	public static final String Data_Selected_City = "Data_Selected_City";
	public static final String First_Selected_City = "First_Selected_City";
	public static final String Location_AreaCode = "Location_AreaCode";
	public static final String Location_City = "Location_City";
	public static final String Location_CityCode = "Location_CityCode";
	public static final String Location_CityId = "Location_CityId";
	public static final String Location_ProvinceCode = "Location_ProvinceCode";
	public static final String NOTIFICATION_SET = "notification_set";
	public static final String SHORT_CUT_INSTALLED = "short_cut_installed";
	public static final String Switch_City_Notice = "Switch_City_Notice";
	public static final String Total_Channel = "total_channel";
	public static SharedPreferences mPreference;
	private static LocationCityListener mLocationCityListener=null;
	
	public static void setmLocationCityListener(LocationCityListener mLocationCityListener)
	{
		SharedUtils.mLocationCityListener = mLocationCityListener;
	}

	public interface LocationCityListener
	{
		public void onLocationCityChange();
	}

	public static String getCurrentCityCode(Context paramContext) {
		return getPreference(paramContext).getString("Location_CityCode", "");
	}

	public static String getCurrentCityId(Context paramContext) {
		return getPreference(paramContext).getString("Location_CityId", "");
	}

	public static String getCurrentCityName(Context paramContext) {
		return getPreference(paramContext).getString("Location_City", "");
	}

	public static String getCurrentProvinceCode(Context paramContext) {
		return getPreference(paramContext).getString("Location_ProvinceCode",
				"");
	}
	
	public static String getCurrentAreaCode(Context paramContext) {
		return getPreference(paramContext).getString("Location_AreaCode",
				"");
	}
	
	public static String getCurrentSimplifyCode(Context paramContext) {
		return getPreference(paramContext).getString("Location_SimplifyCode", "");
	}

	public static long getNotificationHistory(Context paramContext,
			String paramString) {
		return getPreference(paramContext).getLong(paramString, 0L);
	}

	public static int getNotificationSet(Context paramContext) {
		return getPreference(paramContext).getInt("notification_set", 0);
	}

	public static SharedPreferences getPreference(Context paramContext) {
		if (mPreference == null)
			mPreference = PreferenceManager
					.getDefaultSharedPreferences(paramContext);
		return mPreference;
	}

	public static String getUseVersion(Context paramContext) {
		return getPreference(paramContext).getString("USE_VERSION", "");
	}

	public static String getVersionChannelList(Context paramContext) {
		String str1 = getUseVersion(paramContext);
		String str2 = getPreference(paramContext).getString(
				"CHANNEL_VERSION_" + str1, "");
		LogUtils.d("getCityChannelList: CHANNEL_VERSION_" + str1 + ", " + str2);
		return str2;
	}

	public static boolean hasShowHelp(Context paramContext) {
		return getPreference(paramContext).getBoolean("SHOW_HELP", false);
	}

	public static boolean isFirstSelectedCityComplete(Context paramContext) {
		return getPreference(paramContext).getBoolean("First_Selected_City",
				false);
	}

	public static boolean isShortCutInstalled(Context paramContext) {
		return getPreference(paramContext).getBoolean("short_cut_installed",
				false);
	}

	public static boolean isShowSwitchCityNotice(Context paramContext) {
		return getPreference(paramContext).getBoolean("Switch_City_Notice",
				true);
	}

	public static void setCurrentCity(Context paramContext, City paramCity) {
		SharedPreferences localSharePreferences = getPreference(paramContext);
		Editor localEditor = localSharePreferences.edit();
		localEditor.putString("Location_City", paramCity.getCityName());
		localEditor.putString("Location_CityCode", paramCity.getCityCode());
		localEditor.putString("Location_CityId", paramCity.getCityId());
		localEditor.putString("Location_ProvinceCode", paramCity.getProvinceCode());
		localEditor.putString("Location_AreaCode", paramCity.getAreaCode());
		localEditor.putString("Location_SimplifyCode", paramCity.getSimplifyCode());
		LogUtils.d("setCurrentCity================");
		LogUtils.d("cityCode:" + paramCity.getCityCode());
		LogUtils.d("cityName:" + paramCity.getCityName());
		LogUtils.d("areaCode:" + paramCity.getAreaCode());
		LogUtils.d("SimplifyCode:" + paramCity.getSimplifyCode());
		localEditor.commit();
	}
	
	public static void setLocationCity(Context paramContext, City paramCity) {
		try
		{
			SharedPreferences localSharePreferences = Client.getContext().getSharedPreferences("Location_City_Data", 0);
			Editor localEditor = localSharePreferences.edit();
			localEditor.putString("Location_City", paramCity.getCityName());
			localEditor.putString("Location_CityCode", paramCity.getCityCode());
			localEditor.putString("Location_CityId", paramCity.getCityId());
			localEditor.putString("Location_ProvinceCode", paramCity.getProvinceCode());
			localEditor.putString("Location_AreaCode", paramCity.getAreaCode());
			localEditor.putString("Location_SimplifyCode", paramCity.getSimplifyCode());
			LogUtils.d("setCurrentCity================");
			LogUtils.d("cityCode:" + paramCity.getCityCode());
			LogUtils.d("cityName:" + paramCity.getCityName());
			LogUtils.d("areaCode:" + paramCity.getAreaCode());
			LogUtils.d("SimplifyCode:" + paramCity.getSimplifyCode());
			localEditor.commit();
			if(mLocationCityListener!=null)
				mLocationCityListener.onLocationCityChange();
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static City getLastLocationCity()
	{
		City resultCity = null;
		try
		{
			SharedPreferences localSharePreferences = Client.getContext().getSharedPreferences("Location_City_Data", 0);
//		Log.d("getPreferences",localSharePreferences.getString("Location_City", null));
			if (localSharePreferences.getString("Location_City", null) != null)
			{
				resultCity = new City();
				resultCity.setCityName(localSharePreferences.getString("Location_City", ""));
				resultCity.setCityCode(localSharePreferences.getString("Location_CityCode", ""));
				resultCity.setCityId(localSharePreferences.getString("Location_CityId", ""));
				resultCity.setProvinceCode(localSharePreferences.getString("Location_ProvinceCode", ""));
				resultCity.setAreaCode(localSharePreferences.getString("Location_AreaCode", ""));
				resultCity.setSimplifyCode(localSharePreferences.getString("Location_SimplifyCode", ""));
				
//			Log.v("getLoationCity",""+resultCity.getCityName());
			}
			else
			{
//			Log.v("getLoationCity","null");
			}
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return resultCity;
	}
	
	public static City selectCity()
	{
		NativeDBHelper mDB = NativeDBHelper.getInstance(Client.getContext());
		String[] arrayOfString = new String[1];
		arrayOfString[0] = Client.getInstance().area.trim();
		Cursor localCursor = mDB.select(null, "NAME = ?", arrayOfString);
//		Log.d("Cityname", "" + localCursor.moveToFirst());
		if (localCursor.moveToFirst())
		{
			if (localCursor.getString(3).equalsIgnoreCase("3"))
			{
				arrayOfString = new String[2];
				arrayOfString[0] = "2";
				arrayOfString[1] = localCursor.getString(2);
				localCursor = mDB.select(null, "TYPE = ? AND REGION_CODE = ?", arrayOfString);

				if (!localCursor.moveToFirst())
					return null;
			}

			Log.d("Cityname", "" + localCursor.getString(5));
			City localCity = new City();
			localCity.setAreaCode(localCursor.getString(9));
			localCity.setCityCode(localCursor.getString(1));
			localCity.setCityId(localCursor.getString(0));
			localCity.setCityName(localCursor.getString(5));
			localCity.setProvinceCode(localCursor.getString(2));
			SharedUtils.selectComplete(localCity);
			
			return localCity;
		}
		return null;
	}

	public static void selectComplete(City paramCity)
	{
		String simplifyCode = "";
		if (paramCity != null && paramCity.getCityCode() != null)
		{
			simplifyCode = paramCity.getCityCode();
			if (simplifyCode.endsWith("00"))
			{
				simplifyCode = simplifyCode.substring(0, simplifyCode.length() - 2);
			}
			if (simplifyCode.endsWith("00"))
			{
				simplifyCode = simplifyCode.substring(0, simplifyCode.length() - 2);
			}
		}
		paramCity.setSimplifyCode(simplifyCode);
		SharedUtils.setLocationCity( Client.getContext(), paramCity);
	}

	public static void setFirstSelectedCityComplete(Context paramContext) {
		getPreference(paramContext).edit()
				.putBoolean("First_Selected_City", true).commit();
	}

	public static void setNotificationHistory(Context paramContext,
			String paramString, long paramLong) {
		if (!TextUtils.isEmpty(paramString)) {
			getPreference(paramContext).edit().putLong(paramString, paramLong)
					.commit();
			LogUtils.d("SET:" + paramString + " ," + paramLong);
		}
	}

	public static void setNotificationSet(Context paramContext, int paramInt) {
		getPreference(paramContext).edit().putInt("notification_set", paramInt)
				.commit();
	}

	public static void setShortCutInstalled(Context paramContext) {
		getPreference(paramContext).edit()
				.putBoolean("short_cut_installed", true).commit();
	}

	public static void setShowHelp(Context paramContext) {
		getPreference(paramContext).edit().putBoolean("SHOW_HELP", true)
				.commit();
	}

	public static void setSwitchCityNotice(Context paramContext,
			boolean paramBoolean) {
		getPreference(paramContext).edit()
				.putBoolean("Switch_City_Notice", paramBoolean).commit();
	}

	public static void setUseVersion(Context paramContext, String paramString) {
		getPreference(paramContext).edit()
				.putString("USE_VERSION", paramString).commit();
		LogUtils.d("setUseVersion: " + paramString);
	}

	public static void setVersionChannelList(Context paramContext,
			String paramString1, String paramString2) {
		getPreference(paramContext).edit()
				.putString("CHANNEL_VERSION_" + paramString1, paramString2)
				.commit();
		LogUtils.d("setCityChannelList: CHANNEL_VERSION_" + paramString1 + ", "
				+ paramString2);
	}

	public static boolean versionListExist(Context paramContext,
			String paramString) {
		if (getPreference(paramContext).contains(
				"CHANNEL_VERSION_" + paramString))
			LogUtils.d("Version:" + paramString + "Exist!");
		return true;
	}
	
	public static void setCurrentPhoneInfo(Context paramContext, PhoneInfo info) {
		SharedPreferences.Editor localEditor = getPreference(paramContext)
				.edit();
		localEditor.putString("Phone_DeviceId", info.getImei());
		localEditor.putString("Phone_Imsi", info.getImsi());
		localEditor.putString("Phone_No", info.getPhoneNo());
		localEditor.putString("Phone_Provider", info.getProvider());
		localEditor.commit();
	}
	
	public static String getCurrentPhoneDeviceId(Context paramContext) {
		return getPreference(paramContext).getString("Phone_DeviceId", "");
	}
	
	public static String getCurrentPhoneImsi(Context paramContext) {
		return getPreference(paramContext).getString("Phone_Imsi", "");
	}
	
	public static String getCurrentPhoneNo(Context paramContext) {
		return getPreference(paramContext).getString("Phone_No", "");
	}
	
	public static String getCurrentPhoneProvider(Context paramContext) {
		return getPreference(paramContext).getString("Phone_Provider", "");
	}
}